package com.example.culturapp;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapterHoy extends BaseAdapter implements Parcelable{
	private Context context;
    private ArrayList<ItemHoy> items;
 
    public ItemAdapterHoy(Context context, ArrayList<ItemHoy> items) {
        this.context = context;
        this.items = items;
    }
    
    public Context getContext(){
    	return context;
    }
    
    public ArrayList<ItemHoy> getItems(){
    	return items;
    }
    
    /*public ArrayList<String> itemsToStringList(ArrayList<ItemHoy> items){
    	ArrayList<String> result = new ArrayList<String>();
    	for(int i=0; i< items.size();i++){
    		ItemHoy item= items.get(i);
    		result.add(item.getNombre());
    		result.add(item.getDescripcion());
    		result.add(item.getLugar());
    		result.add(item.getFecha());
    		result.add(item.getHora());
    		Object obj =item.getImagen();
    		try{
    			Bitmap bitmap = (Bitmap) obj;
    			Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    			result.add(gson.toJson(bitmap));
    		}catch(Exception e){
    			int j = (Integer) obj;
    			result.add(j+"");
    		}
    	}
    	return result;
    }*/
    
    @Override
    public int getCount() {
        return this.items.size();
    }
 
    @Override
    public Object getItem(int position) {
        return this.items.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
 
        View rowView = convertView;
 
        if (convertView == null) {
            // Create a new view into the list.
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item_hoy, parent, false);
        }
 
        // Set data into the view.
        ImageView ivItemHoy = (ImageView) rowView.findViewById(R.id.ivItem);
        TextView tvTitleHoy = (TextView) rowView.findViewById(R.id.tvTitle);
        TextView tvFecha = (TextView) rowView.findViewById(R.id.tvFecha);
        TextView tvDescripcion = (TextView) rowView.findViewById(R.id.tvDescripcion);
        TextView tvDireccion = (TextView) rowView.findViewById(R.id.tvDireccion);
 
        ItemHoy item = this.items.get(position);
        tvTitleHoy.setText(item.getNombre()+":");
        Bitmap bitmap;
        int img=0;
       	try{
       		bitmap = (Bitmap) item.getImagen();
   		}catch(Exception e){
   			bitmap= null;
   			img = (Integer) item.getImagen();
   		}
       	if(bitmap==null){
       		ivItemHoy.setImageResource(img);
       	}else{
       		ivItemHoy.setImageBitmap(bitmap);
       	}
       tvFecha.setText(" "+item.getFecha()+" - "+item.getHora());
       tvDireccion.setText(" "+item.getLugar());
       tvDescripcion.setText(" "+item.getDescripcion());
       
        return rowView;
    }

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
	}
 
}
